package at.ac.tuwien.prog.memory;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 
 * @author christoph.pickl@student.tuwien.ac.at
 */
class MemoryData {

	private static final Log LOG = LogFactory.getLog(MemoryData.class);

	private final int[] data = new int[16384];

	
	public MemoryData() {
		LOG.info("Creating new MemoryData instance.");
		for (int i = MemoryConstants.DISPLAY_DATA_START; i <= MemoryConstants.DISPLAY_DATA_END; i++) {
			this.data[toAbsoluteAddresses(i)] = (int) ' ';
		}
	}
	
	public int get(int relativeAddress) {
		rangeCheck(relativeAddress);
		final int absoluteAddress = toAbsoluteAddresses(relativeAddress);

		//LOG.trace("getRegisterValue(relativeAddress="+relativeAddress+"); absoluteAddress="+absoluteAddress);
		return this.data[absoluteAddress];
	}
	
	public void set(int relativeAddress, int value) {
//		LOG.debug("setRegisterValue(relativeAddress="+relativeAddress+"; value="+value+")");
		rangeCheck(relativeAddress);
		this.data[toAbsoluteAddresses(relativeAddress)] = value;
	}
	
	private static void rangeCheck(int relativeAddress) {
		// TODO range check
	}
	
	private static int toAbsoluteAddresses(int relativeAddress) {
		return relativeAddress + 8192;
	}
}
